package com.pactera.madp.cp.service.sync.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pactera.madp.cp.service.sync.SyncBatchReplacePartService;
import com.pactera.madp.cp.service.sync.SyncBatchReplaceUserService;
import com.pactera.madp.cp.service.sync.WxCallbackBatchService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.pactera.madp.cp.common.constant.WxCallbackConst.*;

@Slf4j
@Service
@AllArgsConstructor
public class WxCallbackBatchServiceImpl implements WxCallbackBatchService {

    private final SyncBatchReplacePartService batchReplacePartService;
    private final SyncBatchReplaceUserService batchReplaceUserService;

    @Override
    public String dispatchEvent(String dataJson) {
        JSONObject jsonObject = JSON.parseObject(dataJson);
        int corpId = jsonObject.getIntValue("corpId");
        JSONObject xmlJsonObject = jsonObject.getJSONObject("xml");
        JSONObject batchjob = xmlJsonObject.getJSONObject("batchjob");
        String jobtype = batchjob.getString("jobtype");
        String jobId = batchjob.getString("jobid");

        if (JOB_TYPE_SYNC_USER.equals(jobtype)) {
            return "";
        }
        if (JOB_TYPE_REPLACE_USER.equals(jobtype)) {
            log.info("开始回调执行批量同步成员");
            batchReplaceUserService.batchSyncUser();
            return "";
        }
        if (JOB_TYPE_INVITE_USER.equals(jobtype)) {
            return "";
        }
        if (JOB_TYPE_REPLACE_PARTY.equals(jobtype)) {
            log.info("开始回调执行批量同步部门");
            batchReplacePartService.batchSyncDept(jobId);
            return "";
        }
        return "";
    }

}
